import 'dart:convert';

import 'package:dio/dio.dart';

class OscOauthToken {

  int uid;
  int expiresIn;
  String accessToken;
  String refreshToken;
  String tokenType;
  String errorDescription = "OK";
  String error = "1";

  OscOauthToken.fromParams({this.uid, this.expiresIn, this.accessToken, this.refreshToken, this.tokenType});

  factory OscOauthToken(jsonStr){
    if(jsonStr != null) {
      if(jsonStr is Response) {
        var data = jsonStr.data;
        return data is String ? OscOauthToken.fromJson(json.decode(data)) : OscOauthToken.fromJson(data);
      } else {
        return jsonStr is String ? OscOauthToken.fromJson(json.decode(jsonStr)) : OscOauthToken.fromJson(jsonStr);
      }
    } else {
      return OscOauthToken.fromParams();
    }
  }
  OscOauthToken.fromJson(jsonRes) {
    uid = jsonRes['uid'];
    expiresIn = jsonRes['expires_in'];
    accessToken = jsonRes['access_token'];
    refreshToken = jsonRes['refresh_token'];
    tokenType = jsonRes['token_type'];
    error = jsonRes['error']??"1";
    errorDescription = jsonRes['error_description']??"OK";
  }

  @override
  String toString() {
    return '{"uid": $uid,"expiresIn": $expiresIn,'
        '"accessToken": ${accessToken != null?'${json.encode(accessToken)}':'null'},'
        '"refreshToken": ${refreshToken != null?'${json.encode(refreshToken)}':'null'},'
        '"tokenType": ${tokenType != null?'${json.encode(tokenType)}':'null'},'
        '"error": ${error != null?'${json.encode(error)}':'null'},'
        '"errorDescription": ${errorDescription != null?'${json.encode(errorDescription)}':'null'}'
        '}';
  }
}
